<template>
  <!-- 角色表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    style="min-width: 500px; max-width: 700px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="业务领域" prop="businessDomain">
        <el-select
          v-if="!props.isReadonly"
          v-model="formData.businessDomain"
          placeholder="请选择业务领域"
          style="width: 100%"
          clearable
        >
          <el-option
            v-for="item in dictionaryStore.filterDictList('BusinessDomain')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <span v-else>{{
          dictionaryStore.filterDictName(
            "BusinessDomain",
            formData.businessDomain
          )
        }}</span>
      </el-form-item>
      <el-form-item label="参数编号" prop="paramCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.paramCode"
          placeholder="请输入参数编号"
          clearable
        />

        <span v-else>{{ formData.paramCode }}</span>
      </el-form-item>
      <el-form-item label="参数名称" prop="paramName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.paramName"
          placeholder="请输入参数名称"
          clearable
        />

        <span v-else>{{ formData.paramName }}</span>
      </el-form-item>
      <el-form-item label="参数值" prop="paramValue">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.paramValue"
          placeholder="请输入参数值"
          clearable
        />

        <span v-else>{{ formData.paramValue }}</span>
      </el-form-item>
      <el-form-item label="原始值" prop="originalValue">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.originalValue"
          placeholder="请输入原始值"
          clearable
        />

        <span v-else>{{ formData.originalValue }}</span>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.remarks"
          placeholder="请输入备注"
          clearable
        />

        <span v-else>{{ formData.remarks }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "SYSSYSTEMPARAMADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  businessDomain: [
    { required: true, message: "请输入业务领域", trigger: "blur" },
    { min: 1, max: 20, message: "长度在 1 到 20 个字符", trigger: "blur" },
  ],
  paramCode: [
    { required: true, message: "请输入参数编号", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  paramName: [
    { required: true, message: "请输入参数名称", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  paramValue: [
    { required: true, message: "请输入参数值", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  originalValue: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  businessDomain: "",
  paramCode: "",
  paramName: "",
  paramValue: "",
  originalValue: "",
  remarks: "",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        businessDomain,
        paramCode,
        paramName,
        paramValue,
        originalValue,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        businessDomain,
        paramCode,
        paramName,
        paramValue,
        originalValue,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.businessDomain = "";
  formData.paramCode = "";
  formData.paramName = "";
  formData.paramValue = "";
  formData.originalValue = "";
  formData.remarks = "";
  formData.deleted = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

/** 系统参数保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/system/syssystemparam/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
